import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    // 用户信息
    userInfo: {
      id: null,
      nickname: '匿名的宇航员',
      avatar: '🧑‍🚀',
      phone: '',
      email: '',
      signature: ''
    },
    // 登录状态
    isLoggedIn: false,
    // 认证token
    token: '',
    // 每日额度
    dailyQuota: {
      // 今日已投递数量
      sentToday: 0,
      // 今日已捞取数量
      caughtToday: 0,
      // 最大投递数量
      maxSent: 10,
      // 最大捞取数量
      maxCaught: 20,
      // 今日日期
      today: new Date().toDateString()
    }
  }),
  
  getters: {
    /**
     * 获取用户显示名称
     */
    displayName: (state) => {
      return state.userInfo.nickname || '匿名的宇航员'
    },
    
    /**
     * 获取用户头像
     */
    displayAvatar: (state) => {
      return state.userInfo.avatar || '🧑‍🚀'
    },
    
    /**
     * 检查是否可以投递瓶子
     */
    canSendBottle: (state) => {
      // 检查今日是否已更新
      const today = new Date().toDateString()
      if (state.dailyQuota.today !== today) {
        return true // 新的一天，可以投递
      }
      return state.dailyQuota.sentToday < state.dailyQuota.maxSent
    },
    
    /**
     * 检查是否可以捞取瓶子
     */
    canCatchBottle: (state) => {
      // 检查今日是否已更新
      const today = new Date().toDateString()
      if (state.dailyQuota.today !== today) {
        return true // 新的一天，可以捞取
      }
      return state.dailyQuota.caughtToday < state.dailyQuota.maxCaught
    },
    
    /**
     * 获取今日剩余投递次数
     */
    remainingSendCount: (state) => {
      const today = new Date().toDateString()
      if (state.dailyQuota.today !== today) {
        return state.dailyQuota.maxSent
      }
      return state.dailyQuota.maxSent - state.dailyQuota.sentToday
    },
    
    /**
     * 获取今日剩余捞取次数
     */
    remainingCatchCount: (state) => {
      const today = new Date().toDateString()
      if (state.dailyQuota.today !== today) {
        return state.dailyQuota.maxCaught
      }
      return state.dailyQuota.maxCaught - state.dailyQuota.caughtToday
    }
  },
  
  actions: {
    /**
     * 用户登录
     */
    async login(loginData) {
      try {
        // 模拟登录API调用
        console.log('正在登录...', loginData)
        
        // 模拟成功登录
        this.userInfo = {
          id: 'user_' + Date.now(),
          nickname: '匿名的宇航员',
          avatar: '🧑‍🚀',
          phone: loginData.account,
          email: loginData.account,
          signature: '致未来的你，或陌生的灵魂'
        }
        
        this.isLoggedIn = true
        this.token = 'mock_token_' + Date.now()
        
        // 保存到本地存储
        uni.setStorageSync('userInfo', this.userInfo)
        uni.setStorageSync('token', this.token)
        
        return { success: true, message: '登录成功' }
      } catch (error) {
        console.error('登录失败:', error)
        return { success: false, message: '登录失败' }
      }
    },
    
    /**
     * 微信登录
     */
    async wechatLogin() {
      try {
        // 模拟微信登录
        console.log('正在微信登录...')
        
        this.userInfo = {
          id: 'wx_user_' + Date.now(),
          nickname: '微信用户',
          avatar: '🧑‍🚀',
          phone: '',
          email: '',
          signature: '来自微信的宇航员'
        }
        
        this.isLoggedIn = true
        this.token = 'wx_token_' + Date.now()
        
        // 保存到本地存储
        uni.setStorageSync('userInfo', this.userInfo)
        uni.setStorageSync('token', this.token)
        
        return { success: true, message: '微信登录成功' }
      } catch (error) {
        console.error('微信登录失败:', error)
        return { success: false, message: '微信登录失败' }
      }
    },
    
    /**
     * 用户注销
     */
    async logout() {
      try {
        // 清除用户信息
        this.userInfo = {
          id: null,
          nickname: '匿名的宇航员',
          avatar: '🧑‍🚀',
          phone: '',
          email: '',
          signature: ''
        }
        
        this.isLoggedIn = false
        this.token = ''
        
        // 重置每日额度
        this.resetDailyQuota()
        
        // 清除本地存储
        uni.removeStorageSync('userInfo')
        uni.removeStorageSync('token')
        
        return { success: true, message: '注销成功' }
      } catch (error) {
        console.error('注销失败:', error)
        return { success: false, message: '注销失败' }
      }
    },
    
    /**
     * 从本地存储加载用户信息
     */
    loadUserFromStorage() {
      try {
        const userInfo = uni.getStorageSync('userInfo')
        const token = uni.getStorageSync('token')
        
        if (userInfo && token) {
          this.userInfo = userInfo
          this.token = token
          this.isLoggedIn = true
        }
      } catch (error) {
        console.error('加载用户信息失败:', error)
      }
    },
    
    /**
     * 更新用户信息
     */
    async updateUserInfo(newUserInfo) {
      try {
        this.userInfo = { ...this.userInfo, ...newUserInfo }
        
        // 保存到本地存储
        uni.setStorageSync('userInfo', this.userInfo)
        
        return { success: true, message: '用户信息更新成功' }
      } catch (error) {
        console.error('更新用户信息失败:', error)
        return { success: false, message: '更新用户信息失败' }
      }
    },
    
    /**
     * 增加投递次数
     */
    incrementSentCount() {
      this.checkAndResetDailyQuota()
      this.dailyQuota.sentToday++
    },
    
    /**
     * 增加捞取次数
     */
    incrementCatchCount() {
      this.checkAndResetDailyQuota()
      this.dailyQuota.caughtToday++
    },
    
    /**
     * 检查并重置每日额度
     */
    checkAndResetDailyQuota() {
      const today = new Date().toDateString()
      if (this.dailyQuota.today !== today) {
        this.resetDailyQuota()
        this.dailyQuota.today = today
      }
    },
    
    /**
     * 重置每日额度
     */
    resetDailyQuota() {
      this.dailyQuota.sentToday = 0
      this.dailyQuota.caughtToday = 0
      this.dailyQuota.today = new Date().toDateString()
    }
  }
}) 